﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Datos.EntityFramework;
using Entidad;

namespace Datos.DAO
{
    public class ServicioDAO
    {
        BD_HOTEL_Entities bd = new BD_HOTEL_Entities();
        public List<SRH_SERVICIOS> listarServicio()
        {

            List<SRH_SERVICIOS> lista = new List<SRH_SERVICIOS>();
            try
            {
                var q = from c in bd.SRH_SERVICIOS
                        select c;
                lista = q.ToList();

            }
            catch (Exception)
            {
            }
            return lista;
        }

        public bool Insertar(SRH_SERVICIOS Obj)
        {
            try
            {
                bd.AddObject("SRH_SERVICIOS", Obj);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool Modificar(SRH_SERVICIOS obj)
        {
            try
            {
                bd.AttachTo("SRH_SERVICIOS", obj);
                bd.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }

        public bool Eliminar(SRH_SERVICIOS obj)
        {
            try
            {
                bd.AttachTo("SRH_SERVICIOS", obj);
                bd.DeleteObject(obj);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool InsertarDetalle(SRH_DETALLESERVICIO Obj)
        {
            try
            {
                bd.AddObject("SRH_DETALLESERVICIO", Obj);
                bd.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
            return true;
        }

        public int ObtenerNroDetalleServicio()
        {
            int id_opcion = 0;

            try
            {
                var q = (from c in bd.SRH_DETALLESERVICIO
                         select c.iddetalleServicios).DefaultIfEmpty().Max();

                if (q == 0)
                {
                    id_opcion = 100;
                }
                else
                {
                    id_opcion = Convert.ToInt32(q) + 1;
                }

            }
            catch (Exception ex)
            {
                throw;
            }

            return id_opcion;
        }

        public static string Right(string param, int length)
        {

            int value = param.Length - length;
            string result = param.Substring(value, length);
            return result;
        }
        public IEnumerable<Object> listarServicioXReservas(int id)
        {

            IEnumerable<Object> lista = new List<Object>();
            try
            {
                var q = from s in bd.SRH_SERVICIOS
                        from ds in bd.SRH_DETALLESERVICIO
                        where s.idServicios == ds.idServicios && 
                        ds.idReserva==id
                        select new
                        {
                            s.detalle,
                            s.idServicios,
                            s.nombre,
                            s.tarifa,
                            ds.iddetalleServicios,
                            ds.idReserva
                        };
                lista = q.ToList();

            }
            catch (Exception)
            {
            }
            return lista;
        }
    }
}
